package principal.view.gui.model;

import java.util.List;

import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;
import javax.swing.table.DefaultTableModel;

import principal.model.ArticuloComponente;
import principal.model.ArticuloConcreteBuilder;
import principal.model.ArticuloDao;
import principal.model.vos.Articulo;

public class MyTableModel extends DefaultTableModel {
	ArticuloDao artDao = new ArticuloDao();
	
	public MyTableModel(String a){
		
	}
	
	public MyTableModel(){
		
		//Roos2		
		List allArts = artDao.findAllArticulo();
//		List<Articulo> allArts = artDao.findAllArticulo();
		
		
		this.addColumn("Nombre");
		this.addColumn("Descripcion");
		this.addColumn("Precio");
		this.addColumn("Id");
		
		for(int i=0; i<allArts.size(); i++){
			//Roos2
//			this.addRow(new Object[]{arts.getNombre(),arts.getDescripcion(), (arts.getPrecio()), arts.getId(), "\""});			
			this.addRow(new Object[]{((Articulo)allArts.get(i)).getNombre(),((Articulo)allArts.get(i)).getDescripcion(),new Double(((Articulo)(allArts.get(i))).getPrecio()),((Articulo)allArts.get(i)).getId(), "\""});

			
		}
		//ROOS For de arriba
//		for(Articulo arts: allArts){
//			this.addRow(new Object[]{arts.getNombre(),arts.getDescripcion(), arts.getPrecio(), arts.getId(), "\""});
//			
//		}
		
	}
	
	public void refresca(){

		
		//Roos2
		List allArts = artDao.findAllArticulo();
//		List<Articulo> allArts = artDao.findAllArticulo();
		
		setRowCount(0);
		
		for(int i=0; i<=allArts.size(); i++){			
			//Double precio = (Articulo)allArts.get(i)).getPrecio();
//			this.addRow(new Object[]{arts.getNombre(),arts.getDescripcion(), arts.getPrecio(), arts.getId(), "\""});
			this.addRow(new Object[]{((Articulo)allArts.get(i)).getNombre(),((Articulo)allArts.get(i)).getDescripcion(),new Double(((Articulo)(allArts.get(i))).getPrecio()),((Articulo)allArts.get(i)).getId(), "\""});
			
		}
		
		//ROOS For de arriba
//		for(Articulo arts: allArts){
//			this.addRow(new Object[]{arts.getNombre(),arts.getDescripcion(), arts.getPrecio(), arts.getId(), "\""});
//			
//		}
	}
	

       /*
        * JTable uses this method to determine the default renderer/
        * editor for each cell.  If we didn't implement this method,
        * then the last column would contain text ("true"/"false"),
        * rather than a check box.
        */
       //public Class getColumnClass(int c) {
         //  return getValueAt(0, c).getClass();
       //}

       /*
        * Don't need to implement this method unless your table's
        * editable.
        */
       public boolean isCellEditable(int row, int col) {
           //Note that the data/cell address is constant,
           //no matter where the cell appears onscreen.
           if (col < 2) {
               return false;
           } else {
               return true;
           }
       }

       /*
        * Don't need to implement this method unless your table's
        * data can change.
        */


       private void printDebugData() {
           int numRows = getRowCount();
           int numCols = getColumnCount();

           for (int i=0; i < numRows; i++) {
               System.out.print("    row " + i + ":");
              
               System.out.println();
           }
           System.out.println("--------------------------");
       }
       
       
 
       
   }

